package com.local.admin.controller;

import com.local.admin.constants.AdminConstants;
import com.local.common.constants.BusinessException;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.ResultUtil;
import com.local.goods.feign.GoodsServiceFeign;
import com.local.goods.pojo.GoodsCategory;
import com.local.goods.pojo.GoodsCategoryDeleteRequestParam;
import com.local.goods.pojo.GoodsCategoryListRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 商品分类管理控制器
 */
@Controller
@RequestMapping("/admin/category")
public class AdminGoodsCategoryController {

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

    /**
     * 分类列表页面
     * @param categoryLevel
     * @param parentId
     * @param backParentId
     * @param request
     * @return
     */
    @GetMapping
    public String index(
            @RequestParam("categoryLevel") Integer categoryLevel,
            @RequestParam("parentId") Integer parentId,
            @RequestParam("backParentId") Integer backParentId,
            HttpServletRequest request
    ) {
        if (Objects.isNull(categoryLevel) || categoryLevel < 0 || categoryLevel > 3) {
            throw new BusinessException(ExceptionCodeEnum.BUSINESS_EXCEPTION.getCode(), "参数错误");
        }

        request.setAttribute("path", "category");
        request.setAttribute("categoryLevel", categoryLevel);
        request.setAttribute("parentId", parentId);
        request.setAttribute("backParentId", backParentId);

        return "admin/category";
    }


    /**
     * 查询分类数据，支持翻页
     * @param categoryLevel
     * @param parentId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam("categoryLevel") Byte categoryLevel, @RequestParam("parentId") Integer parentId,
                           @RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam("sidx") String sort, @RequestParam("order") String order) {
        if (Objects.isNull(categoryLevel) || categoryLevel.toString().isEmpty()
                || Objects.isNull(parentId) || parentId.toString().isEmpty()) {
            return new ResultUtil(201, "参数错误");
        }
        sort = Objects.isNull(sort) || sort.isEmpty() ? "category_rank" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "asc" : order;
        GoodsCategoryListRequestParam requestParam = new GoodsCategoryListRequestParam(categoryLevel, parentId, page, limit, sort, order, -1);
        ResultUtil categoryResult = goodsServiceFeign.categoryList(requestParam);
        return categoryResult;
    }


    /**
     * 新增
     * @param goodsCategory
     * @param session
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody GoodsCategory goodsCategory, HttpSession session) {
        Integer adminUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        goodsCategory.setCreateUser(adminUserId);
        goodsCategory.setUpdateUser(adminUserId);
        goodsCategory.setCreateTime(dateTime);
        goodsCategory.setUpdateTime(dateTime);
        ResultUtil saveResult = goodsServiceFeign.categorySave(goodsCategory);
        return saveResult;
    }


    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody GoodsCategory goodsCategory, HttpSession session) {
        Integer adminUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        goodsCategory.setUpdateUser(adminUserId);
        goodsCategory.setUpdateTime(dateTime);
        ResultUtil updateResult = goodsServiceFeign.categoryUpdate(goodsCategory);
        return updateResult;
    }


    /**
     * 批量删除
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(@RequestBody Integer[] ids, HttpSession session) {
        Integer adminUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        GoodsCategoryDeleteRequestParam requestParam = new GoodsCategoryDeleteRequestParam(ids, adminUserId, dateTime);
        ResultUtil deleteResult = goodsServiceFeign.categoryDelete(requestParam);
        return deleteResult;
    }


    /**
     * 后台商品管理中，通过parent id选择下级分类列表
     * @param categoryId
     * @return
     */
    @GetMapping("/listForSelect")
    @ResponseBody
    public ResultUtil listForSelect(@RequestParam("categoryId") Integer categoryId) {
        if (Objects.isNull(categoryId) || categoryId < 1) {
            return new ResultUtil(201, "参数错误");
        }
        ResultUtil result = goodsServiceFeign.categoryListForSelect(categoryId);
        return result;
    }
}
